package com.example.test.chapter10.contextmenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.chapter10.MainChapter10;

public class ContextMenuAActivity extends Activity {
	String[] phones = { "HTC Rezound", "Samsung Galaxy S II Skyrocket",
			"Samsung Galaxy Nexus", "Motorola Droid Razr", "Samsung Galaxy S",
			"Samsung Epic Touch 4G", "iPhone 4S", "HTC Titan" };
	private static final int MENU_NEW_GAME = Menu.FIRST;
	private static final int MENU_QUIT = Menu.FIRST + 1;
	private static final int MENU_EDIT = Menu.FIRST + 2;
	private static final int MENU_DELETE = Menu.FIRST + 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maincontextmenu);
		ListView listView = (ListView) findViewById(R.id.list_context_menu);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.listitem, phones);
		listView.setAdapter(adapter);
		registerForContextMenu(listView);
		Button btn = (Button) findViewById(R.id.backpage);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MainChapter10.class);
				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.list_context_menu) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle(phones[info.position]);
			menu.add(0, MENU_EDIT, 0, "Edit");
			menu.add(0, MENU_DELETE, 0, "Delete");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_NEW_GAME, 0, "New Game");
		menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_icon);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_NEW_GAME:
			return true;
		case MENU_QUIT:
			return true;
		}
		return false;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		TextView textView = (TextView) findViewById(R.id.footer_contex_menu);
		switch (item.getItemId()) {
		case MENU_EDIT:
			textView.setText("Edit selected");
			return true;
		case MENU_DELETE:
			textView.setText("Delete selected");
			return true;
		default:
			return super.onContextItemSelected(item);
		}

	}
}
